axios.defaults.baseURL = 'http://api-breakingnews-web.itheima.net';

// 设置拦截器，配置token认证

// 请求拦截器
axios.interceptors.request.use(function (config) {

    if (config.url.indexOf('/my') !== -1) {
        config.headers.Authorization = localStorage.getItem('token')
    }
    return config

}, function (error) {
    return Promise.reject(error)
});


// 响应拦截器
axios.interceptors.response.use(function (response) {

    if (response.data.message === '身份认证失败！') {
        location.href = '/login.html'
        localStorage.removeItem('token')
    }
    return response

}, function (error) {
    return Promise.reject(error)

});

// 格式化日期方法
function formData(time) {
    let dt = new Date(time)
    let y = dt.getFullYear()
    let m = padZero(dt.getMonth() + 1)
    let d = padZero(dt.getDate())

    let hh = padZero(dt.getHours())
    let mm = padZero(dt.getMinutes())
    let ss = padZero(dt.getSeconds())
    return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
}

// 日期补零

function padZero(n) {
    return n < 10 ? '0' + n : n
}